import requests
from bs4 import BeautifulSoup
import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    db='zf',
    user='root',
    passwd='root',
    charset='utf8')
cur = conn.cursor()

def main():
    url = 'http://ssfw.gzcourt.gov.cn:8080/ssfw/webapp/area/gz/gggs/gggs_index.jsp'
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Cookie': 'JSESSIONID=F72E1DB591DD22C432BF5E06DABDC8A6; JSESSIONID=1D4F89FC22A7F33AB08186C10FD0CFF7',
        'Host': 'ssfw.gzcourt.gov.cn:8080',
        'Referer': 'http://ssfw.gzcourt.gov.cn:8080/ssfw/webapp/area/gz/gggs/gggs_index.jsp?currentPage=7',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
    }
    for i in range(1,101):
        # url = url + str(i)
        params = {
            'currentPage': str(i),
        }
        html = requests.get(url=url,headers=headers,params=params).text
        soup = BeautifulSoup(html,'lxml')
        rs = soup.select('.pageTable tr')
        if len(rs) == 21:
            for tag in rs[1::2]:
                data = tag.get_text()
                data = data.strip().split('\n')
                info = (int(int(data[0])+(i-1)*((len(rs)-1)/2)),data[1],data[2],data[3],data[4])
                sql = "insert into tb_zf values(%s,%s,%s,%s,%s)"
                cur.execute(sql,info)
                conn.commit()
                print("%s插入成功"%((info,)))
    cur.close()
    conn.close()


if __name__ == '__main__':
    main()